package com.flying.watch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

public class PubClient {

    static String server = "/servers";

    public static void main(String[] args) throws Exception {
//        String hosts = "192.168.106.13:2181";
        String hosts = "127.0.0.1:2181";
        ZooKeeper zk = new ZooKeeper(hosts, 30000, null);
        // 如果"/servers"不存在,则创建
        if (zk.exists(server, false) == null) {
            zk.create(server, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        // 注册服务
        zk.create(server + "/" + "server1", "192.168.1.1:8080".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        Thread.sleep(5000);
        zk.close();
    }
}